package com.anyunlink.example.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.anyunlink.example.entity.VacationForm;
import com.anyunlink.example.service.MiaoService;
import com.anyunlink.util.ResultInfo;


@Controller
public class MiaoController {
	@Autowired
	private MiaoService miaoService;
	
	@Autowired
	private IdentityService identityService;
	
	@GetMapping( "/")
	public String login(){
		return "login";
	}
	@ResponseBody
	@PostMapping( "/login")
	public Object login(HttpServletRequest request, HttpServletResponse response,String username){
		ResultInfo result = new ResultInfo();
		Group group = identityService.createGroupQuery().groupMember(username).singleResult();
		
		if(group != null) {
			String groupName  = group.getName();
			if("employee".equals(groupName)) {
				result.setUserType(1);
			}else if("boss".equals(groupName)) {
				result.setUserType(2);
			}
			
			result.setCode(200);
			result.setMsg("登录成功");
			//用户信息存放在Cookie中，实际情况下保存在Redis更佳
			Cookie cookie = new Cookie("userInfo", username);
			cookie.setPath("/");
			response.addCookie(cookie);
		}else {
			result.setCode(300);
			result.setMsg("登录名不存在，登录失败");
		}
		return result;
	}
	@ResponseBody
	@GetMapping("/logout")
	public ResultInfo logout(HttpServletRequest request, HttpServletResponse response) {
		ResultInfo result = new ResultInfo();
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("userInfo")) {
					cookie.setValue(null);
					// 立即销毁cookie
					cookie.setMaxAge(0);
					cookie.setPath("/");
					response.addCookie(cookie);
					break;
				}
			}
		}
		result.setCode(200);
		return result;
	}
	
	//添加请假单
	@ResponseBody
	@PostMapping( "/writeForm")
	public Object writeForm(HttpServletRequest request,String title,String content,String operator){
		
		ResultInfo result = new ResultInfo();
		VacationForm form = miaoService.writeForm(title,content,operator);
		result.setCode(200);
		result.setMsg("填写请假条成功");
		result.setInfo(form);
		return result;
	}
	
	//申请者放弃请假
	@ResponseBody
	@GetMapping( "/giveup")
	public ResultInfo giveup(HttpServletRequest request,String formId,String operator){
		ResultInfo result = new ResultInfo();
		miaoService.completeProcess(formId, operator, "giveup");
		result.setCode(200);
		result.setMsg("放弃请假成功");
		return result;
	}
	
	//申请者申请请假
	@ResponseBody
	@GetMapping( "/apply")
	public ResultInfo apply(HttpServletRequest request,String formId,String operator){
		ResultInfo result = new ResultInfo();
		miaoService.completeProcess(formId, operator, "apply");
		result.setCode(200);
		result.setMsg("申请请假成功");
		return result;
	}
	
	//审批者审核请假信息
	@ResponseBody
	@GetMapping( "/approve")
	public ResultInfo approve(HttpServletRequest request,String formId,String operator){
		ResultInfo result = new ResultInfo();
		miaoService.approverVacation(formId, operator);
		result.setCode(200);
		result.setMsg("请假审核成功");
		return result;
	}
	
	//获取某条请假信息当前状态
	@GetMapping( "/currentState")
	public HashMap<String,String> currentState(HttpServletRequest request){
		String formId = request.getParameter("formId");
		HashMap<String,String> map = new HashMap<String,String>();
		map = miaoService.getCurrentState(formId);
		return map;
	}
	@ResponseBody
	@GetMapping( "/historyState")
	public ResultInfo queryHistoricTask(HttpServletRequest request,String formId){
		ResultInfo result = new ResultInfo();
		List<Map<String,String>> process = miaoService.historyState(formId);
		result.setCode(200);
		result.setInfo(process);
		return result;
    }
	//申请者的首页
	@GetMapping( "/home")
	public String index(ModelMap model, HttpServletRequest request){
		List<VacationForm> forms = miaoService.formList();
		Cookie[] cookies = request.getCookies();
		String user = "";
		//从cookie中获取当前用户
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("userInfo")) {
					user = cookie.getValue();
					break;
				}
			}
		}
		List<HashMap<String, Object>> formsMap = new ArrayList<HashMap<String, Object>>();
		for(VacationForm form : forms) {
			//申请者只能看到自己申请的请假单信息
			if(user.equals(form.getApplicant())) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("id", form.getId());
				map.put("title", form.getTitle());
				map.put("content", form.getContent());
				map.put("applicant", form.getApplicant());
				map.put("state", form.getState());
				formsMap.add(map);
			}
		}
		//将forms参数返回
		model.addAttribute("forms",formsMap);
		return "index";
	}
	
	//审核者的首页
	@GetMapping( "/homeApprover")
	public String indexApprover(ModelMap model){
		List<VacationForm> forms = miaoService.formList();
		List<HashMap<String, Object>> formsMap = new ArrayList<HashMap<String, Object>>();
		for(VacationForm form : forms) {
			//审核者只能看到待审核状态的请假单
			if("领导审核".equals(form.getState())) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("id", form.getId());
				map.put("title", form.getTitle());
				map.put("content", form.getContent());
				map.put("applicant", form.getApplicant());
				map.put("state", form.getState());
				formsMap.add(map);
			}
		}
		model.addAttribute("forms",formsMap);
		return "indexApprover";
	}
	
	//请假单页面
	@GetMapping( "/form")
	public String form(){
		return "form";
	}
}
